package com.xhwl.centiotdata.repair.imp;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.enums.pedestrian.PedestrianMethodEnum;
import com.xhwl.centiotdata.repair.IRepairTask;
import com.xhwl.centiotdata.service.integrateconfig.PedestrianIntegrationConfig;
import com.xhwl.common.dto.cent.iotdata.repair.RepairActionDTO;
import com.xhwl.common.enums.iot.RepairTypeEnum;
import com.xhwl.common.utils.CalendarUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (C),1998-2022, 兴海物联
 * FileName: PedestrianSyncTask
 * Author: XuChang
 * Date: 2022/8/9 19:07
 * Description: 人行修复任务
 * History:
 */
@Component
public class PedestrianSyncTask implements IRepairTask<PedestrianIntegrationConfigDTO> {

    PedestrianIntegrationConfig integrationConfig;

    @Autowired
    public void setIntegrationConfig(PedestrianIntegrationConfig integrationConfig) {
        this.integrationConfig = integrationConfig;
    }

    @Override
    public int getType() {
        return RepairTypeEnum.PEDESTRIAN.getType();
    }

    @Override
    public PedestrianIntegrationConfigDTO getConfig(RepairActionDTO repairActionDTO) {
        List<PedestrianIntegrationConfigDTO> integrationConfigDTOList = integrationConfig.getIntegrationConfig(null).getOrDefault(PedestrianMethodEnum.RECORD_STATISTICS_LAST_DAY.getId(), Collections.emptyList());
        List<PedestrianIntegrationConfigDTO> collect = integrationConfigDTOList.stream()
                .filter(config -> repairActionDTO.getProjectId().equals(config.getProjectId()))
                .collect(Collectors.toList());
        return CollectionUtils.isEmpty(collect) ? null : collect.get(0);
    }

    @Override
    public String repairAction(Date date, PedestrianIntegrationConfigDTO pedestrianIntegrationConfigDTO) {
        PedestrianIntegrationHandler handler = PedestrianIntegrationFactory.getInvokeStrategy(pedestrianIntegrationConfigDTO.getProductId());
        if (handler == null)
            return "未找到对应handler配置";
        String dateStr = CalendarUtil.getDateStr(CalendarUtil.DATE_FORMAT[1], date);
        return handler.repairRecord(pedestrianIntegrationConfigDTO, dateStr);
    }

}
